package com.study.lxy.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;

//@Component
public class MyCompentAutowiredRegistrar implements ImportBeanDefinitionRegistrar,ResourceLoaderAware,BeanFactoryAware {

    private ResourceLoader resourceLoader;

    private BeanFactory beanFactory;

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        // 由于ClassPathBeanDefinitionScanner的doScan()方法是protected类型的，不能直接使用，所以自己定义一个Scanner
        MyCompentAutoScanner myCompentAutoScanner = new MyCompentAutoScanner(registry,false);
        myCompentAutoScanner.setResourceLoader(resourceLoader);
        myCompentAutoScanner.doFilter();
        myCompentAutoScanner.doScan("com.study.lxy");
    }
}
